Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use button tag and native tabindex for clear button #63

Merged

Conversation

vtx-anton-chashchin
Copy link
Contributor

@vtx-anton-chashchin vtx-anton-chashchin commented Jan 15, 2024

Summary by CodeRabbit

  • 新特性

    • 清除图标从 <span> 元素更改为 <button> 元素,提高了可访问性和交互性。
  • 样式

    • 更新了 .rc-input-clear-icon 的样式,移除了填充、背景和边框。
  • 测试

    • 重新组织了 BaseInput 组件的测试,增加了多个清除输入的交互场景。

Copy link

codecov bot commented Jan 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.34%. Comparing base (c0dd6ed) to head (152a4c0).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #63   +/-   ##
=======================================
  Coverage   92.34%   92.34%           
=======================================
  Files           5        5           
  Lines         222      222           
  Branches       79       77    -2     
=======================================
  Hits          205      205           
  Misses         17       17           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mellis481
Copy link

@afc163 @MadCcc @zombieJ Can you please review this PR? It's an important a11y improvement that my team needs. Thanks!

@mellis481
Copy link

@afc163 @MadCcc @zombieJ bump

@mellis481
Copy link

@afc163 @MadCcc @zombieJ Can you please merge this PR?

Copy link

coderabbitai bot commented Nov 29, 2024

Walkthrough

此次更改涉及到三个主要文件,分别是 assets/index.lesssrc/BaseInput.tsxtests/BaseInput.test.tsx。在 assets/index.less 中,修改了 .rc-input 组件的样式,特别是清除图标的样式。src/BaseInput.tsx 文件将清除图标从 <span> 元素更改为 <button> 元素,以提升可访问性和交互性,同时保留了相关的事件处理逻辑。在 tests/BaseInput.test.tsx 中,测试用例进行了重构,以更好地模拟用户交互并测试清除功能。

Changes

文件路径 更改摘要
assets/index.less 修改 .rc-input 组件的 .rc-input-clear-icon 样式,设置 padding0,移除 cursor,并将 backgroundborder 设置为 none
src/BaseInput.tsx 将清除图标从 <span> 元素更改为 <button> 元素,移除不必要的 roletabIndex 属性,保留事件处理逻辑。
tests/BaseInput.test.tsx 重构 allowClear 功能的测试,增加多个 it 测试以评估不同的清除交互方式,使用 userEvent 模拟用户行为。

Possibly related PRs

Poem

在草地上跳跃的小兔子,
清除图标变得更可爱,
按钮替代了小小的标记,
交互更友好,真是精彩!
测试用例也更清晰,
让我们一起欢呼,真是美丽! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (5)
assets/index.less (1)

25-28: 清除按钮样式修改符合无障碍设计要求

样式更改合理地移除了按钮的默认外观,同时保持了视觉一致性。这些更改与将清除图标从 <span> 改为 <button> 的改动相匹配,有效地提升了组件的可访问性。

建议考虑添加以下样式来进一步优化按钮的交互体验:

 .rc-input-clear-icon {
   padding: 0;
   font-size: 12px;
   background: none;
   border: none;
+  cursor: pointer;
+  &:focus {
+    outline: 1px solid #000;
+    outline-offset: 2px;
+  }
 }
src/BaseInput.tsx (1)

Line range hint 83-97: 使用原生button标签改善了可访问性,代码变更合理!

将清除图标从span改为button标签是一个很好的改进:

  • 提供了原生键盘交互支持
  • 不再需要显式设置tabIndex和role属性
  • 符合语义化HTML最佳实践

建议考虑添加以下无障碍增强:

  • 为按钮添加aria-label,以提供更好的屏幕阅读器支持
  • 添加type="button"属性,防止在表单中意外提交
 <button
+  type="button"
+  aria-label="清除输入内容"
   onClick={(event) => {
     handleReset?.(event);
     onClear?.();
   }}
tests/BaseInput.test.tsx (3)

80-98: 建议使用 userEvent 简化点击测试

当前测试使用了多个独立的鼠标事件(mouseDown、click、mouseUp),建议使用 userEvent 来简化测试代码,使其更接近真实用户交互。

建议修改如下:

-      fireEvent.mouseDown(clearIcon!);
-      fireEvent.click(clearIcon!);
-      fireEvent.mouseUp(clearIcon!);
+      await user.click(clearIcon!);

100-126: 建议优化键盘交互测试以减少重复代码

Space 和 Enter 键的测试逻辑非常相似,建议使用 test.each 模式来减少代码重复。

建议重构如下:

-    it('By focus and Space', async () => {
-      const { container } = render(<Demo />);
-
-      const inputEl = container.querySelector('input');
-      await user.click(inputEl!);
-
-      await user.type(inputEl!, 'some text');
-      expect(inputEl!.value).toBe('some text');
-
-      await user.tab();
-      await user.keyboard('[Space]');
-      expect(inputEl!.value).toBe('');
-    });
-
-    it('By focus and Enter', async () => {
-      const { container } = render(<Demo />);
-
-      const inputEl = container.querySelector('input');
-      await user.click(inputEl!);
-
-      await user.type(inputEl!, 'some text');
-      expect(inputEl!.value).toBe('some text');
-
-      await user.tab();
-      await user.keyboard('[Enter]');
-      expect(inputEl!.value).toBe('');
-    });
+    it.each([
+      ['Space', '[Space]'],
+      ['Enter', '[Enter]']
+    ])('By focus and %s', async (name, key) => {
+      const { container } = render(<Demo />);
+
+      const inputEl = container.querySelector('input');
+      await user.click(inputEl!);
+
+      await user.type(inputEl!, 'some text');
+      expect(inputEl!.value).toBe('some text');
+
+      await user.tab();
+      await user.keyboard(key);
+      expect(inputEl!.value).toBe('');
+    });

Line range hint 49-126: 建议添加焦点管理测试

当前测试用例没有验证清除操作后输入框的焦点状态。为了确保良好的用户体验,建议添加相关测试。

建议添加如下测试:

it('应保持输入框焦点在清除后', async () => {
  const { container } = render(<Demo />);
  const inputEl = container.querySelector('input');
  
  await user.type(inputEl!, 'some text');
  await user.tab();
  await user.keyboard('[Enter]');
  
  expect(document.activeElement).toBe(inputEl);
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between c0dd6ed and 152a4c0.

⛔ Files ignored due to path filters (1)
  • tests/__snapshots__/index.test.tsx.snap is excluded by !**/*.snap
📒 Files selected for processing (3)
  • assets/index.less (1 hunks)
  • src/BaseInput.tsx (2 hunks)
  • tests/BaseInput.test.tsx (3 hunks)
🔇 Additional comments (4)
src/BaseInput.tsx (2)

Line range hint 84-91: 事件处理逻辑设计合理!

点击事件和鼠标按下事件的处理方式值得肯定:

  • 通过preventDefault()防止失焦,解决了#31200问题
  • 同时调用内部handleReset和外部onClear保持了良好的扩展性

Line range hint 91-96: 样式类名处理得当!

类名逻辑保持了组件的一致性:

  • 正确处理了清除按钮的显示/隐藏状态
  • 与后缀图标的样式兼容性良好
  • 保持了原有的样式系统
tests/BaseInput.test.tsx (2)

2-2: 引入 userEvent 是个很好的改进!

使用 @testing-library/user-event 可以更真实地模拟用户交互,这是一个值得肯定的改进。


49-55: 测试结构优化得当!

将 allowClear 相关测试组织在一个 describe 块中,并共享测试设置,这样的重构提高了代码的可维护性和可读性。

@afc163 afc163 merged commit 220a5e3 into react-component:master Nov 29, 2024
7 checks passed
@afc163
Copy link
Member

afc163 commented Nov 29, 2024

https://github.com/react-component/input/releases/tag/v1.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants